/* window.cc
* This file is part of ncursesxx library
* Copyright (c) 2003,2004 by Stanislav Ievlev
*
* This file is covered by the GNU Library General Public License,
* which should be included with libncursesxx as the file COPYING.
*/
#include <stdexcept>

#include <ncurses/window.hh>
#include <ncurses/assert.hh>

using namespace ncurses;

window::window():
		counter_(),
		win_(0)
{
	win_ = ::newwin(1, 1, 0, 0);
	if (!win_) throw std::runtime_error("newwin call failed");
	::keypad(win_, TRUE); //initialize advanced keycodes for this window
}

window::window(const window& other):
		counter_(other.counter_),
		win_(other.win_)
{}

window::~window()
{
	if (1 == *counter_.usage_) ::delwin(win_);
}

void window::swap(window& other)
{
	std::swap(win_, other.win_);
	counter_.swap(other.counter_);
}

window& window::operator=(const window& other)
{
	window(other).swap(*this);
	return *this;
}

void window::set_scroll(bool res)
{
	scrollok(win_, res); //don't use ::, 'cause scrollok may be a macro
}

void window::resize(unsigned int w, unsigned int h)
{
	NCURSES_ASSERT(::wresize(win_, h, w));
}

unsigned int window::width() const
{
	int y, x;
	getmaxyx(win_, y, x);
	return x;
}

unsigned int window::height() const
{
	int y, x;
	getmaxyx(win_, y, x);
	return y;
}

unsigned int window::x() const
{
	int y, x;
	getbegyx(win_, y, x);
	return x;
}

unsigned int window::y() const
{
	int y, x;
	getbegyx(win_, y, x);
	return y;
}

void window::touch_line(int num, int count)
{
	::touchline(win_, num, count);
}

void ncurses::copy(window& source,window& destination)
{
    	NCURSES_ASSERT(::copywin(source.win(), destination.win(), 0, 0, 0, 0,
				 source.height() - 1, source.width() - 1, FALSE));
}
